//
//  CLHomePageView.m
//  CLBrowser
//
//  Created by 梁慧聪 on 2017/10/26.
//  Copyright © 2021年 梁慧聪. All rights reserved.
//

#import "CLHomePageView.h"
#import "CLVCSearch.h"
#import "CLVCMain.h"

@interface CLHomePageView()

@end

@implementation CLHomePageView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self initializeView];
    }
    return self;
}

- (void)initializeView{
    self.backgroundColor = UIColorFromRGB(0xF8F8F8);
    
    UILabel *lbTitle = [[UILabel alloc]initWithFrame:CGRectMake(0, 177, self.width, 50)];
    [self addSubview:lbTitle];
    lbTitle.text = @"CLBrower";
    lbTitle.textColor = HEXRGB(0x3AB54A);
    lbTitle.textAlignment = NSTextAlignmentCenter;
    lbTitle.font = [UIFont boldSystemFontOfSize:36];
    
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.alignment = NSTextAlignmentLeft;
    NSAttributedString *attributedString = [[NSAttributedString alloc]initWithString:@"搜索或输入网址" attributes:@{NSForegroundColorAttributeName:HEXRGB(0x858585),NSFontAttributeName:[UIFont systemFontOfSize:14],NSParagraphStyleAttributeName:style}];
    
    
    NSInteger leftMargin = 57;
    UITextField *tfInput = [[UITextField alloc]initWithFrame:CGRectMake(leftMargin, lbTitle.bottom + 20, self.width - leftMargin * 2, 35)];
    CGSize size = [attributedString boundingRectWithSize:tfInput.size options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;
    [self addSubview:tfInput];
    UIView *leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,( tfInput.width - size.width)/2, 14)];
    UIImageView *ivSearch = [[UIImageView alloc]initWithFrame:CGRectMake(leftView.width - 14, 0, 14, 14)];
    ivSearch.image = [UIImage imageNamed:@"antOutline-search"];
    [leftView addSubview:ivSearch];
    tfInput.leftView = leftView;
    tfInput.leftViewMode = UITextFieldViewModeAlways;
    tfInput.layer.cornerRadius = 5;
    tfInput.layer.borderColor = HEXRGB(0x3AB54A).CGColor;
    tfInput.layer.borderWidth = 1;
    
    tfInput.attributedPlaceholder = attributedString;
    [tfInput addTarget:self action:@selector(handleSearchButtonClicked) forControlEvents:UIControlEventEditingDidBegin];
}

- (void)handleSearchButtonClicked {
    [self endEditing:YES];
    [Notifier postNotification:[NSNotification notificationWithName:kExpandHomeToolBarNotification object:nil]];
    CLVCSearch *searchVC = [[CLVCSearch alloc]init];
    searchVC.origTextFieldString = @"";
    [CLNavigationService pushController:searchVC animated:YES];
}

@end
